<template>
  <div id="app">
    <!-- 顶部栏 -->
    <base-header></base-header>

    <!-- 首页 -->
    <base-index></base-index>

    <!-- 介绍 -->
    <base-intro></base-intro>

    <!-- 代币 -->
    <base-token></base-token>

    <!-- 路线图 -->
    <base-path></base-path>
  </div>
</template>

<script>
import { getUrlParams } from '@/assets/js/utils'
import VConsole from 'vconsole'
import web3 from '@/assets/js/web3'
import baseHeader from '@/components/base_header.vue'
import baseIndex from '@/components/base_index.vue'
import baseIntro from '@/components/base_intro.vue'
import baseToken from '@/components/base_token.vue'
import basePath from '@/components/base_path.vue'

export default {
  mounted() {
    if (getUrlParams().debug) {
      new VConsole()
    }

    this.getConfigInfo()

    web3.init().then(() => {
      web3.getUserAddress().then((userAddress) => {
        console.log('userAddress：', userAddress)
        this.$store.commit('setUserAddress', userAddress)
        this.checkIsRegister(userAddress)
        this.getUserInfo()
        this.$children[2].$children[0].submitRegister()
      })
    })
  },

  methods: {
    /**
     * 检查是否已注册
     */
    checkIsRegister(userAddress = '') {
      if (userAddress) {
        web3.initContract('IDO').then(() => {
          web3.checkIsRegister(userAddress).then((flag) => {
            this.$store.commit('setIsRegister', flag)
          })
        })
      }
    },

    /**
     * 获取配置信息
     */
    async getConfigInfo() {
      const { code, data } = await this.$apis.getConfigInfo({
        loading: false,
      })

      if (code === 200) {
        this.$store.commit('setConfigInfo', data)
      }
    },

    /**
     * 获取用户信息
     */
    async getUserInfo() {
      const params = {
        walletAddress: this.$store.state.userAddress,
      }

      const { code, data } = await this.$apis.getUserInfo(params)

      if (code === 200) {
        this.$store.commit('setUserInfo', data || {})
      }
    },
  },

  components: {
    baseHeader,
    baseIndex,
    baseIntro,
    baseToken,
    basePath,
  },
}
</script>

<style lang="scss">
@import '@/assets/style/index.scss';
</style>
